<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org"
	  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
	  lang="en">
	<head>
		<meta charset="utf-8">
		<title>Layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="/layui-v2.6.4/layui/css/layui.css" media="all">
		<script src="/layui-v2.6.4/layui/layui.js" charset="utf-8"></script>
		<script src="/js/jquery-3.5.1.min.js" charset="UTF-8"></script>
	</head>
	<body>

		<table class="layui-hide" id="test" lay-filter="test"></table>

		<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
				<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
				<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
				<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
				<button class="layui-btn layui-btn-sm" id="addfiction">新增</button>
			</div>
		</script>

		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
			<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="onblocked">封禁</a>
		</script>

		<script>
			layui.use('table', function() {
				var table = layui.table;
				table.render({
					elem: '#test',
					url: 'getUsers',
					parseData: function (res) { //res 即为原始返回的数据
						return {
							"code": res.code, //解析接口状态
							"msg": res.msg, //解析提示文本
							"count": res.count, //解析数据长度
							"data": res.data //解析数据列表
						}
					},
					page: true,
					limit: 10,
					limits: [3, 5, 10, 20],
					toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
					height: 'full-3',
					defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
						title: '提示',
						layEvent: 'LAYTABLE_TIPS',
						icon: 'layui-icon-tips'
					}],
					title: '用户数据表',
					cols: [
						[{
							type: 'checkbox',
							fixed: 'left'
						}, {
							field: 'id',
							title: 'ID',
							width: 80,
							fixed: 'left',
							unresize: true
						}, {
							field: 'userName',
							title: '用户姓名',
							width: 180,
							edit: 'text'
						}, {
							field: 'userPhone',
							title: '联系方式',
							width: 200,
							edit: 'text'
						}, {
							field: 'userPwd',
							title: '用户密码',
							width: 150,
							edit: 'text'
						}, {
							field: 'userLevel',
							title: '用户等级',
							width: 120,
							sort: true
						},{
							field: 'gmtCreate',
							title: '注册时间',
							width: 120,
							sort: true
						}, {
							field: 'gmtModified',
							title: '最后登录时间',
							width: 120,
							sort: true
						}, {
							field: 'isBanned',
							title: '状态',
							width: 80
						}, {
							fixed: 'right',
							title: '操作',
							toolbar: '#barDemo',
							width: 200
						}]
					]
				});

				table.on('toolbar(test)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'getCheckData':
							var data = checkStatus.data;
							layer.alert(JSON.stringify(data));
							break;
						case 'getCheckLength':
							var data = checkStatus.data;
							layer.msg('选中了：' + data.length + ' 个');
							break;
						case 'isAll':
							layer.msg(checkStatus.isAll ? '全选' : '未全选');
							break;

							//自定义头工具栏右侧图标 - 提示
						case 'LAYTABLE_TIPS':
							layer.alert('这是工具栏右侧自定义的一个图标按钮');
							break;
					};
				});

				//监听行工具事件
				table.on('tool(test)', function(obj) {
					var data = obj.data;
					if (obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							layer.msg('权限不足!!');
						});
					} else if (obj.event === 'edit') {
						layer.prompt({
							formType: 2,
							value: data.email
						}, function (value, index) {
							obj.update({
								email: value
							});
							layer.close(index);
						});
					} else if (obj.event === 'onblocked') {
						var userid = data.id;
						var userblocked = data.isBanned;
						$.ajax({
							url: "/onlock"
							, type: 'get'
							, async: false
							, data: {
								id: userid, isBlock: userblocked
							}
							, success: function (res) {
								if (res === "1") {
									layer.alert("封锁成功");
									obj.update({
										isBanned: 1
									});
								} else if (res === "0") {
									layer.alert("解封成功")
									obj.update({
										isBanned: 0
									});
								}
							}
						});
					}
				});
			});
		</script>

	</body>
</html>
